﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using KUMA.CellularAutomata.HelperLibrary;

namespace KUMA.CellularAutomata
{
    public partial class MainForm : Form
    {
        //所有细胞 包括坐标和生存状态
        List<Cell> AllCells=new List<Cell>(); 

        public MainForm()
        {
            InitializeComponent();
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            this.label1.Text = "宽：" + this.Spc.Panel2.Width.ToString();
            this.label2.Text = "高：" + this.Spc.Panel2.Height.ToString();
            DrawBoard();//初始化默认的细胞盘
        }

        private void MainForm_Paint(object sender, PaintEventArgs e)
        {
            
        }


        //绘制底板，底色为黑色，填充默认的白色死亡细胞底
        private void DrawBoard()
        {
            SplitterPanel sPanel = this.Spc.Panel2;
            Graphics g = sPanel.CreateGraphics();
            Brush brush = new SolidBrush(Color.White);
            int RectLength = AutoConfig.CellLength + 1;
            List<Cell> InitCells = new List<Cell>(); 
            int Reci = 0;
            Rectangle[] Rects = new Rectangle[sPanel.Width / RectLength * sPanel.Height / RectLength];
            for (int i = 0; i < sPanel.Width / RectLength; i++)
            {
                for (int j = 0; j < sPanel.Height / RectLength; j++)
                {
                    Rects[Reci] = new Rectangle(RectLength * i + 1, RectLength * j + 1, AutoConfig.CellLength, AutoConfig.CellLength);
                    ///绘制底色白色的细胞 为死亡
                    InitCells.Add(new Cell() { CellID = Reci, CellX = i, CellY = j, IsAlive = false });
                    Reci++;
                }
            }
            if (AllCells.Count==0)
            {
                AllCells = InitCells;
            }
            g.FillRectangles(brush, Rects);
        }

        //初始的4个存活细胞
        private void InitAliveCell()
        {
            Cell c=new Cell(){ CellX = 100,CellY = 100};
               AllCells.FindAll(o => ((o.CellX >= c.CellX - 1 && o.CellX <= c.CellX + 1)
                                   && (o.CellY >= c.CellY - 1 && o.CellY <= c.CellY + 1)))
                   .ToArray().ToList().ForEach(o => o.IsAlive = true);
        }

        private void DrawCell()
        {
            SplitterPanel sPanel = this.Spc.Panel2;
            Graphics g = sPanel.CreateGraphics();
            int RectLength = AutoConfig.CellLength + 1;
            List<Cell> AliveCells = AllCells.FindAll(o => o.IsAlive == true);
            List<Cell> DeadCells = AllCells.FindAll(o => o.IsAlive == false);
            Rectangle[] AliveRect = new Rectangle[AliveCells.Count];
            Rectangle[] DeadRect = new Rectangle[DeadCells.Count];
            int alivei = 0;
            int deadi = 0;
            foreach (Cell cell in AllCells)
            {
                Rectangle CellRect = new Rectangle(RectLength * cell.CellX + 1, RectLength * cell.CellY + 1,
                    AutoConfig.CellLength, AutoConfig.CellLength);
                if (cell.IsAlive)
                {
                    AliveRect[alivei] = CellRect;
                    alivei++;
                }
                else
                {
                    DeadRect[deadi] = CellRect;
                    deadi++; 
                }
            }
            try{g.FillRectangles(new SolidBrush(Color.Red), AliveRect);}catch (Exception){}
            try { g.FillRectangles(new SolidBrush(Color.White), DeadRect); }catch (Exception) {  }
           
            
        }

        private void btnInitialize_Click(object sender, EventArgs e)
        {
            //初始化细胞列表
            AllCells = new List<Cell>();
            DrawBoard();
            InitAliveCell();
            DrawCell();
        }

        private void btnRun_Click(object sender, EventArgs e)
        {
            
        }


        private List<Cell> LoopRun(List<Cell> OldList)
        {
            List<Cell> ret = new List<Cell>();
            double ListLength = OldList.Count;
            //初始左标 0 后续覆盖
            double LeftIndex = 0;
            //初始右标 为长度/8取整;
            double RightIndex = Math.Floor(ListLength / 8);

            for (int i = 0; i < 8; i++)
            {
                List<Cell> TheCell = ret.FindAll(o => o.CellID < RightIndex && o.CellID > LeftIndex).ToList();
            }
            return ret;
        }
    }
}
